const os = require('os');

// 获取本地IP地址
function getLocalIP() {
    const interfaces = os.networkInterfaces();
    for (const devName in interfaces) {
        const iface = interfaces[devName];
        for (let i = 0; i < iface.length; i++) {
            const alias = iface[i];
            if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.internal) {
                return alias.address;
            }
        }
    }
    return 'localhost';
}

module.exports = {
    devServer: {
        host: '0.0.0.0', // 允许外部访问
        port: 8080,      // 端口号，可根据需要修改
        open: false      // 不自动打开浏览器
    },
    chainWebpack: config => {
        // 自定义启动信息
        config.plugin('friendly-errors').tap(args => {
            const localIP = getLocalIP();
            const port = process.env.port || 8080;

            // 清除默认消息
            args[0].messages = [];

            // 添加自定义消息
            args[0].messages.push(`  App running at:`);
            args[0].messages.push(`  - Local:   http://localhost:${port}`);
            args[0].messages.push(`  - Network: http://${localIP}:${port}`);

            return args;
        });
    }
};
